﻿using Forum.BaseController;
using Forum.Model.QueryFilters;
using Forum.Service.Interface.NoAuth;

namespace Forum.Controllers.NoAuth
{
    /// <summary>
    /// 通知
    /// </summary>
    [Route("Api/Notice")]
    public class NoticeController : NoAuthController<INoticeService>
    {
        public NoticeController(INoticeService defaultService, ILogger<NoAuthController<INoticeService>> logger) : base(defaultService, logger)
        {
        }

        /// <summary>
        /// 查询通知（分页）
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        public ApiResponsePaged<Notice> GetPageList([FromQuery] QueryParameters param)
        {
            var pagedList = DefaultService.GetPageList(param);
            return new ApiResponsePaged<Notice>
            {
                data = pagedList.ToList(),
                total = pagedList.Total,
                haveMore = pagedList.Total > param.Page * param.PageSize
            };
        }

        /// <summary>
        /// 批量查询通知
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResponse GetNoticeList()
        {
            var list = DefaultService.GetNoticeList();
            return ApiResponse.OK("", list);
        }

        /// <summary>
        /// 查询通知消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id:int}")]
        public ApiResponse GetNotice(int id)
        {
            var notice = DefaultService.GetNotice(id);
            return ApiResponse.OK("", notice);
        }

        /// <summary>
        /// 更新通知（改成已读）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("{id:int}")]
        public ApiResponse UpdateNotice(int id)
        {
            var isSuccess = DefaultService.UpdateNotice(id);
            return isSuccess ? ApiResponse.OK() : ApiResponse.Error("更新异常");
        }
    }
}
